﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using AllInOnePluginBL.Helpers;
using Rss;

namespace AllInOnePluginBL.SiteScrappers
{
    /// <summary>
    /// Summary description for CuevanaTV
    /// </summary>
    public class CuevanaTV
    {
        private const string DEFAULT_URL = "http://www.cuevana.tv/";
        private const string MOVIES_URL = "peliculas";
        private const string SERIES_URL = "series";

        public CuevanaTV()
        {
            //this.Name = "cuevana.tv";
        }

        #region Private Methods
        private RssFeed LoadCategories(string url)
        {
            try
            {
                var page = Utils.GetPage(url, Encoding.UTF8);

                var sectioMatch = Regex.Match(page, "<div id=\"submenu\">(.+?)</div>", RegexOptions.Singleline);
                if (!sectioMatch.Success)
                    throw new Exception("Failed Parse Movies categories section");

                var categoriesSection = sectioMatch.Groups[1].Value;

                const string moviesContainerExpression = "<li><a href=\"(.+?)\".*?<span.*?>(.+?)<";
                var matches = Regex.Matches(categoriesSection, moviesContainerExpression, RegexOptions.Singleline);
                foreach (Match match in matches)
                {
                    var pageLink = WrapUrl(match.Groups[1].Value);
                    var title = match.Groups[2].Value;

                    //movie.MovieLinks.Add(pageLink);

                    //var movieItems = movie.GetRssItems(Utils.GetSiteUrl(), this._pluginId, (int)PluginMode.MOVIES_MODE);
                    //foreach (var movieItem in movieItems)
                    //{
                    //    rssChannel.Items.Add(movieItem);
                    //}
                }

                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private RssFeed LoadMovies(string url)
        {
            try
            {
                var page = Utils.GetPage(url, Encoding.UTF8);

                var videosRegEx = "<tr class='row[^<]+";
                videosRegEx += "<td valign='top'><a href='([^']+)'><img src='([^']+)'[^>]+></a></td>[^<]+";
                videosRegEx += "<td valign='top'><div class='tit'><a[^>]+>([^<]+)</a></div>[^<]+";
                videosRegEx += "<div class='font11'>([^<]+)<";

                var matches = Regex.Matches(page, videosRegEx, RegexOptions.Singleline);
                foreach (Match match in matches)
                {
                    var moviePageLink = WrapUrl(match.Groups[1].Value);
                    var imageLink = match.Groups[2].Value;
                    var movieTitle = match.Groups[3].Value;
                    var plot = match.Groups[4].Value;

                    //var movie = new Movie
                    //                {
                    //                    Name = movieTitle,
                    //                    ImageLink = imageLink,
                    //                    Plot = plot,
                    //                    MovieLinks = new List<string> {moviePageLink}
                    //                };

                    //var movieItems = movie.GetRssItems(Utils.GetSiteUrl(), this._pluginId, (int)PluginMode.MOVIE_MODE);
                    //foreach (var movieItem in movieItems)
                    //{
                    //    rssChannel.Items.Add(movieItem);
                    //}
                }

                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private string GetNextPageLink(string pageContent)
        {
            try
            {
                const string nextPageExpression = "<a class='next' href='(.+?)'";
                var match = Regex.Match(pageContent, nextPageExpression, RegexOptions.Singleline);
                if (match.Success)
                {
                    //if (this.Url.Contains(MOVIES_URL))
                    //    return string.Format("{0}{1}/{2}", DEFAULT_URL, MOVIES_URL, match.Groups[1].Value.Trim());
                    return string.Format("{0}{1}/{2}", DEFAULT_URL, SERIES_URL, match.Groups[1].Value.Trim());
                }
            }
            catch { }

            return string.Empty;
        }
        
        private string WrapUrl(string url)
        {
            return string.Format("{0}{1}", DEFAULT_URL, url); 
        }
        #endregion
    }
}